package com.seari.ibpPanleMidware.utils;

public class ByteUtil {
	private ByteUtil(){}
	
	//取高八位
	public static int getHighByte(int num){
		return 0xffff&(num>>8);
	}
	
	//取低八位
	public static int getLowByte(int num){
		return (0xffff&(num<<8))>>8;
	}
	
	/**
	 * 转成控制字符串
	 * @param type
	 * @param info
	 * @return
	 * 0文字字体
	 * 1字体颜色
	 * 2字体大小
	 * 3对齐方式
	 * 4出字间隔
	 * 5图片编码   图片类型
	 * 6
	 */
	public static String getControlStr(String type,String info){
		String returnMes = "";
		switch(type){
		case "0":returnMes="1b39"+Integer.toHexString(0x30+Integer.parseInt(info,16));break;
		case "1":returnMes="1b"+Integer.toHexString(32+Integer.parseInt(info,10));break;
		case "2":returnMes="1b3a"+Integer.toHexString(0x30+Integer.parseInt(info,10));break;
		case "3":returnMes="1b"+Integer.toHexString(48+Integer.parseInt(info.split(",")[0],10))+"1b"+Integer.toHexString(48+Integer.parseInt(info.split(",")[1],10));break;
		case "4":returnMes="1b38"+Integer.toHexString(0x30+Integer.parseInt(info.substring(0,1),10))+""+Integer.toHexString(0x30+Integer.parseInt(info.substring(1,2),10))+""+Integer.toHexString(0x30+Integer.parseInt(info.substring(2,3),10));break;
		case "5":returnMes="1b36"+(0x30+Integer.parseInt(info.split(",")[0], 16))+""+(0x30+Integer.parseInt(info.split(",")[1], 16));break;
		//case "6":;break;
		}
		return returnMes;
	}
	
	public static String getHexStr(int[]  num){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<num.length;i++){
			String hString = (Integer.toHexString(getHighByte(num[i]))+"").toUpperCase();
			String lString = (Integer.toHexString(getLowByte(num[i]))+"").toUpperCase();
			if(hString.length()==1){
				sb.append("0"+hString);
			}else{
				sb.append(hString);
			}
			if(lString.length()==1){
				sb.append("0"+lString);
			}else{
				sb.append(lString);
			}
			
		}
		return sb.toString();
	}
	
	public static int[] getUnEmptyArr(int[] arr){
		int num=0;
		for(int i=0;i<arr.length;i++){
			if(arr[i]>0){
				num = i;
			}
		}
		int[] unEmpty=new int[num+1];
		System.arraycopy(arr,0,unEmpty, 0, num+1);
		return unEmpty;
	}
	
	public static String hexString2binaryString(String hexString)  
    {  
        if (hexString == null || hexString.length() % 2 != 0)  
            return null;  
        String bString = "", tmp;  
        for (int i = 0; i < hexString.length(); i++)  
        {  
            tmp = "0000"  
                    + Integer.toBinaryString(Integer.parseInt(hexString  
                            .substring(i, i + 1), 16));  
            bString += tmp.substring(tmp.length() - 4);  
        }  
        return bString;  
    } 
		
}
